package com.jrx.anytxn.transaction.job.autoPayment.step;

import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import com.jrx.anytxn.transaction.entity.TlPaymentMapping;
import com.jrx.anytxn.transaction.job.autoPayment.bean.PaymentLogBean;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlPaymentLogMapper;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlPaymentMappingMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 按客户级扣款批扣准备writer
 *
 * @author : zhenghaopu
 * 2019/11/22
 */
@Service
public class AutoPaymentPreWriter implements ItemWriter<PaymentLogBean> {

    @Autowired
    private ExtTlPaymentMappingMapper extTlPaymentMappingMapper;

    @Autowired
    private ExtTlPaymentLogMapper extTlPaymentLogMapper;

    @Override
    public void write(List<? extends PaymentLogBean> items) {
        List<TlPaymentLog> tlPaymentLogs =new ArrayList<>(items.size());
        List<TlPaymentMapping> tlPaymentMappings=new ArrayList<>(items.size());

        for(PaymentLogBean paymentLogBean:items){
            if(CollectionUtils.isNotEmpty(paymentLogBean.getTlPaymentLogList())){
                tlPaymentLogs.addAll(paymentLogBean.getTlPaymentLogList());
            }
            if(paymentLogBean.getTlPaymentLog()!=null){
                tlPaymentLogs.add(paymentLogBean.getTlPaymentLog());
            }
            tlPaymentMappings.addAll(paymentLogBean.getTlPaymentMappingList());
        }
        if(!CollectionUtils.isEmpty(items)){
            if (!CollectionUtils.isEmpty(tlPaymentLogs)) {
                extTlPaymentLogMapper.batchInsert(tlPaymentLogs);
            }
            if (!CollectionUtils.isEmpty(tlPaymentMappings)) {
                extTlPaymentMappingMapper.batchInsert(tlPaymentMappings);
            }
        }
    }
}
